<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
	<base href="<%=basePath%>">
	<title>增加单位</title>
	<script src="<%=basePath%>component/layout.js"></script>
	<script src="<%=basePath%>component/toolbar.js"></script>
	<script src='<%=basePath%>dwrsystem/interface/unitService.js'></script>
	<script src='<%=basePath%>dwrsystem/engine.js'></script>
	<script src="<%=basePath%>js/validateJS.js"></script>
</head>

<body>
	<div id="displayDiv" class="formlayout" style="overflow:auto;width:100%;height:100%;">
		<table id="addTable" >
			<tr>
				<th>单位名称 ：</th>
				<td height="27">
					<input type="hidden" name="tunit.superUnitId" id="tunit.superUnitId"/>
					<input name="tunit.unitName" id="tunit.unitName" maxlength='25'><span class="enterInput">*</span>
				</td>
			</tr>
			<tr>
				<th>备注：</th>
				<td>
					<textarea rows="3" cols="80" name="tunit.note" id="tunit.note"></textarea>
				</td>
			</tr>
		</table>
	</div>
</body>
	
<script type="text/javascript">
var nowurl = '${param.nowurl}';
var superUnitId = '${param.unitId}';
var dhxLayout = createLayoutNormal('1C',false,true);

$(function(){
	configLayout();
});

function configLayout(){
	var a = dhxLayout.cells('a');
	dhxLayout.progressOn();
	a.hideHeader();
	a.setText('主页面');
	a.fixSize(true, true);
	createToolbarFromDHXLayoutCell(a,function(toolbar){
	    toolbar.addText("info", 1, "操作:");
	    toolbar.addSeparator("sep1", 2);
	    if(power.getPowerInsert())toolbar.addButton("add", 3, "提交", "save.gif", "save_dis.gif");
	    toolbar.addButton("cancel", 4, "取消", "undo.gif", "undo_dis.gif");
	    toolbar.attachEvent("onClick", function(id) {
	    	if(id == 'add'){
	    		validateAdd();
	    	}
	    	if(id == 'cancel'){
	    		backAdd();
	    	}
	    });
	});
	a.attachObject('displayDiv');
	dhxLayout.progressOff();
}

function validateAdd(){
	var uname = document.getElementById('tunit.unitName').value;
	if(trim(uname)==''){
		if(nowurl == 'listtop')
			parent.parent.notifyWarn('请输入单位名称');
		else
			parent.parent.parent.notifyWarn('请输入单位名称');
		return;
	}
	if (!validatePattern.validateInputZZ(uname,false)){
		if(nowurl == 'listtop')
			parent.parent.notifyWarn('单位名称非法，输入只能是[汉字,数字、26个英文字母或者下划线]');
		else
			parent.parent.parent.notifyWarn('单位名称非法，输入只能是[汉字,数字、26个英文字母或者下划线]');
		return ;
	}
	var unote = document.getElementById('tunit.note').value;
	layoutProgressOn(dhxLayout);
	unitService.hasTUnitName({superUnitId:superUnitId, unitName:uname}, function(data){
		if(data == 1){
			if(nowurl == 'listtop')
				parent.parent.notifyWarn('已存在相同名称的单位');
			else
				parent.parent.parent.notifyWarn('已存在相同名称的单位');
			layoutProgressOff(dhxLayout);
			return;
		}else{
			var mytree = parent.parent.window.MyTree;//标签页中寻找tree
			if(nowurl == 'edit'){ // 增加子单位
				var ulevel = mytree.getNodeLevel(superUnitId) + 1;
				unitService.insertTUnit({unitName:uname, note:unote, superUnitId:superUnitId, unitLevel:ulevel}, function(uid){
					mytree.addNode(superUnitId, uid, uname, 0, 0, 0, 0, "SELECT,CALL");
		   		});
			}else if(nowurl == 'list'){ // 增加子单位
				var ulevel = mytree.getNodeLevel(superUnitId) + 1;
				unitService.insertTUnit({unitName:uname, note:unote, superUnitId:superUnitId, unitLevel:ulevel}, function(uid){
					mytree.addNode(superUnitId, uid, uname, 0, 0, 0, 0, "");
					document.location.href = '<%=basePath%>page/system/unit/unit_list.jsp?superUnitId=' + superUnitId;
		   		});
			}else if(nowurl == 'listtop'){ // 增加顶级单位
				unitService.insertTUnit({unitName:uname, note:unote, superUnitId:'0', unitLevel:1}, function(uid){
					parent.window.MyTree.addNode(0, uid, uname, 0, 0, 0, 0, "");
					document.location.href = '<%=basePath%>page/system/unit/unit_listtop.jsp?superUnitId=0';
		   		});
			}
			
		}
	});
}

function backAdd(){
	layoutProgressOn(dhxLayout);
	if(nowurl == 'edit'){
		document.location.href = '<%=basePath%>system-unit/UnitAction!unitQueryForEdit.action?unitId=' + superUnitId;
	}else if(nowurl == 'list'){
		document.location.href = '<%=basePath%>page/system/unit/unit_list.jsp?superUnitId=' + superUnitId;
	}else if(nowurl == 'listtop'){
		document.location.href = '<%=basePath%>page/system/unit/unit_listtop.jsp?superUnitId=0';
	}
}
</script>
	
</html>
